<?
require("Questionary.php");

define("FILENAME", ($_SERVER['PHP_SELF'].(($_SERVER['QUERY_STRING'])? '?'.$_SERVER['QUERY_STRING'] : '')));

$conn = mysql_pconnect('localhost', 'root', '') or die ('Error connecting to mysql!!');
mysql_select_db('youknow');

session_start();

if (isset($_POST['login']))
{
	if ($_POST['username'])
	{
		$result = mysql_query('SELECT * FROM user WHERE username = "'.$_POST['username'].'" AND password = PASSWORD("'.$_POST['password'].'")');

		if (mysql_num_rows($result))
		{
			$row = mysql_fetch_assoc($result);

			$_SESSION['userid'] = $row['id'];
			$_SESSION['username'] = $row['username'];
			$_SESSION['fullname'] = $row['fullname'];
			$_SESSION['questionary'] = new Questionary();
		}
		else
		{
			$loginerror = 'User or password incorrect!!!';
		}
	}
}

if (isset($_POST['logout']))
{
	session_destroy();
	$_SESSION = array();
}

define("LOGIN", (isset($_SESSION['username'])? true : false));

if (LOGIN)
{
	if (isset($_POST['questionary']))
	{		
		foreach ($_POST as $index => $value)
		{
			$index_piece = explode('_', $index);

			if ($index_piece[0] == 'question')
			{
				mysql_query('INSERT INTO user_answer VALUES("", "'.$_SESSION['userid'].'", "'.$value.'", NOW())');
				$correct = $_SESSION['questionary']->setAnswer($index_piece[1], $value);
			}
		}
		$_SESSION['questionary']->correct();
		
		$title_result = 'Results: '.$_SESSION['questionary']->getPercentage().'% ('.$_SESSION['questionary']->getTotalRight().' of '.$_SESSION['questionary']->getTotal().')';
	}
	else
	{
		if (isset($_GET['size']))
		{
			$_SESSION['size'] = $_GET['size'];
			$_SESSION['questionary']->load($_GET['size'], true);
		}
		else
		{
			$_SESSION['size'] = 0;
			$_SESSION['questionary']->clean();
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

	<head>
		<link rel="stylesheet" href="default.css" type="text/css" />
		<title>youknow</title>
	</head>

	<body>
	
<?php if (LOGIN): /////////////////////////////////////////////////////////// ?>
		<form id="login" name="login" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
			<?= $_SESSION['username'] ?> | 
			<a href="<?= $_SERVER['PHP_SELF'] ?>">Statistic</a> | 
			<a href="javascript: document.login.submit()">Options</a> | 
			<a href="javascript: document.login.submit()">Logout</a>
			<input name="logout" type="hidden" value="1" />
		</form>
		
<?php endif; //////////////////////////////////////////////////////////////// ?> 
	
		<img id="logo" src="images/youknow.gif" alt="youknow" />
	
<?php if (!LOGIN): ////////////////////////////////////////////////////////// ?>
		<form id="login2" name="login" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
			<div class="accountaccess">
				Acesse a sua conta
			</div>	
			Login:<br />
			<input id="username" name="username" type="text" size="15" value="<?= (isset($_POST['username']))? $_POST['username'] : '' ?>" class="text" /><br />
			Password:<br />
			<input id="password" name="password" type="password" size="15" class="text" /><br />
			<div class="buttonbox">
				<input id="submit" name="login" type="submit" value="Sign In" class="button" />
			</div>
			<hr />
			<div class="forgotpassword">
				<a href="">Esqueceu sua senha?</a>
			</div>
<?php if (isset($loginerror)) { ?>
			<span id="loginerror"><?= $loginerror ?></span>
<?php } ?>
		</form>
<?php endif; //////////////////////////////////////////////////////////////// ?> 

		<div id="main">

			<hr />

<?php if (LOGIN): /////////////////////////////////////////////////////////// ?>
		<div class="bar">

				<input id="submit_questionary" type="button" value="Correct" onclick="javascript: document.questionary.submit()" />
		
				<form id="create_questionary" method="get" action="<?= FILENAME ?>">
					Exam:
					<select name="exam">
						<option value="lpi">LPI</option>
					</select>
					
					Size:
					<select name="size">
<?php foreach (array(1, 5, 10) as $size) { ?>
						<option value="<?= $size ?>"<?= ($_SESSION['size'] == $size)? ' selected="selected"' : '' ?>><?= $size ?></option>
<?php } ?>
					</select>
		
					<input type="submit" value="OK" class="button" />
				</form>
				
			</div>

<?php
if ($_SESSION['questionary']->getTotal())
{
	$_SESSION['questionary']->printHTML(3, false);
}
else
{
	$query = mysql_query('SELECT COUNT(*) FROM question_user WHERE user_id = '.$_SESSION['userid']);
	
	
	$questions_answered_total = mysql_result(mysql_query('SELECT COUNT(*) FROM question_user WHERE user_id = '.$_SESSION['userid']), 0);
	$questions_answered_right = mysql_result(mysql_query('SELECT COUNT(*) FROM question_user WHERE user_id = '.$_SESSION['userid'].' AND correct = "Y"'), 0);
	$questions_answered_wrong = mysql_result(mysql_query('SELECT COUNT(*) FROM question_user WHERE user_id = '.$_SESSION['userid'].' AND correct = "N"'), 0);
	$questions_mean = round(($questions_answered_right*100)/$questions_answered_total);
?>
			<table id="statistic">
				<tr>
					<td class="good_number">3&ordm;</td>
					<td class="normal">is your <a href="?ranking">ranking</a>.</td>
				</tr>
				<tr>
					<td class="good_number"><?= $questions_answered_total ?></td>
					<td class="normal">questions answered since 21/10/2008.</td>
				</tr>
				<tr>
					<td class="good_number"><?= $questions_answered_right ?></td>
					<td class="normal">questions answered right.</td>
				</tr>
				<tr>
					<td class="bad_number"><?= $questions_answered_wrong ?></td>
					<td class="normal">questions answered wrong.</td>
				</tr>
				<tr>
					<td class="good_number"><?= $questions_mean ?>%</td>
					<td class="normal">is your mean.</td>
				</tr>
			</table>
<?php
}
?>

			<div class="bar">
				
				<input id="submit_questionary" type="button" value="Correct" onclick="javascript: document.questionary.submit()" />
		
				<form id="create_questionary" method="get" action="<?= FILENAME ?>">
					Exam:
					<select name="exam">
						<option value="lpi">LPI</option>
					</select>
					
					Size:
					<select name="size">
<?php foreach (array(1, 5, 10) as $size) { ?>
						<option value="<?= $size ?>"<?= ($_SESSION['size'] == $size)? ' selected="selected"' : '' ?>><?= $size ?></option>
<?php } ?>
					</select>
		
					<input type="submit" value="OK" class="button" />
				</form>
				
			</div>

<?php else: ///////////////////////////////////////////////////////////////// ?>
<?php
$questions_total = mysql_result(mysql_query('SELECT COUNT(*) FROM question'), 0);
$users_total = mysql_result(mysql_query('SELECT COUNT(*) FROM user'), 0);
?>
			<table id="statistic">
				<tr>
					<td class="good_number"><?= $questions_total ?></td>
					<td class="normal">questions.</td>
				</tr>
				<tr>
					<td class="good_number">12</td>
					<td class="normal">questions answered.</td>
				</tr>
				<tr>
					<td class="good_number"><?= $users_total ?></td>
					<td class="normal">users.</td>
				</tr>
			</table>
<?php endif; //////////////////////////////////////////////////////////////// ?>

			<hr />
			
		</div>

	</body>

</html>
